﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.cms.presentation.Trees;
using System.Web;
using System.IO;

namespace Umb.SimpleWebsite.Shared.DataTypes.Trees.FileSO
{
    public class FSOTree:BaseTree
    {
        public FSOTree(string alias) : base(alias) { }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.NodeType = "init" + TreeAlias;
            rootNode.NodeID = "-1";
            this.NodeKey=string.IsNullOrEmpty(this.NodeKey)? "":this.NodeKey;
            GetXmlTreeFolderNode(ref rootNode, this.NodeKey);
        }

        public override void Render(ref XmlTree tree)
        {
            this.NodeKey = string.IsNullOrEmpty(this.NodeKey) ?  "" : this.NodeKey;
            DirectoryInfo rd = new DirectoryInfo(Path.Combine(AppPath, this.NodeKey));
                if (rd.Exists)
                {
                    foreach (DirectoryInfo ditem in rd.GetDirectories())
                    {
                        XmlTreeNode node = XmlTreeNode.Create(this);
                        GetXmlTreeFolderOrFileNode(ref node, ditem,true);
                        tree.Add(node);
                    }
                    foreach (FileInfo fitem in rd.GetFiles())
                    {
                        XmlTreeNode node = XmlTreeNode.Create(this);
                        GetXmlTreeFolderOrFileNode(ref node, fitem,false);
                        tree.Add(node);
                    }

                }
            
        }

        private void GetXmlTreeFolderNode(ref XmlTreeNode node,string path)
        {
            path = Path.Combine(AppPath, path);
            FileSystemInfo fsi= new DirectoryInfo(path);
            bool isfolder = true;
            if (!fsi.Exists) { fsi = new FileInfo(path); isfolder = false; }
            if (!fsi.Exists) node.Text = "Node haven't be found!";
            else {
                GetXmlTreeFolderOrFileNode(ref node, fsi, isfolder);
            }
        }
        private void GetXmlTreeFolderOrFileNode(ref XmlTreeNode node, FileSystemInfo fsi,bool isFolder)
        {
            node.Text = fsi.Name;
            node.NodeID = fsi.Name.ToLowerInvariant();
            node.Icon = isFolder?FolderIcon:"doc.gif";
            node.OpenIcon = isFolder ? FolderIconOpen : "doc.gif";
            node.HasChildren = isFolder;
            string relativepath = fsi.FullName.Replace(AppPath, "");
            node.Source = this.GetTreeServiceUrl(relativepath);
            node.NodeType = (isFolder?"folder_":"file") + TreeAlias;
            node.Action = "javascript:openFileEx('" + relativepath.Replace("\\","\\\\") + "');";
        }


        private string apppath;
        public string AppPath
        {
            get
            {
                if (string.IsNullOrEmpty(apppath)) apppath = HttpContext.Current.Request.PhysicalApplicationPath;
                return apppath; }
        }
        public override void RenderJS(ref StringBuilder Javascript)
        {
            Javascript.Append("<!--javascript filesys object tree-->");
        }

    }
}
